function [fb_snips_av,pb_snips_av,fb_snips_out,pb_snips_out,thresh]=pert_ana_new2(aux_data,frame_times,ROIs,nbr_frames,draw_raw,draw_vel,draw_last,roi,pl)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   mean pert (local and full field) response per cell
%   For 1 layer exp use only the first 2 arguments
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fig_pos=[25   477   612   327];
z=4;
ids=[-1:-1:-6 1:7];
l_win=15;
r_win=25;
% curZ=[];
% curLen=0;
% cnt=1;
% while isempty(curZ)==1
%     curLen=curLen+length(ROIs{cnt});
%     if roi>curLen
%         cnt=cnt+1;
%     else
%         if cnt>1
%             curZ(1,:)=[roi-(curLen-length(ROIs{cnt})),cnt];
%         else
%             curZ(1,:)=[roi,cnt];
%         end
%     end
% end

if nargin>7
    curZ(1,:)=[roi,pl];
    cur_frame_times=frame_times(curZ(1,2):4:end);
    act=ftfil(ROIs{curZ(1,2)}(curZ(1,1)).activity,18,0,10);
    act=psmooth(act);
else
    curZ(1,1)=roi;
    cur_frame_times=frame_times;
    act=ftfil(ROIs(curZ(1,1)).activity,18,0,10);
    act=psmooth(act);
end
[velP,velM,velP_ind,velM_ind,velP_raw,velM_raw]=get_vel_ind_from_adata(aux_data);
stim_id=round(aux_data(3,:)*2);
shutterOpen=find(abs(diff(aux_data(1,:)>1))==1);

% Code:
% 1 = feedback
% 2 = playback
% 3 = baseline

if length(nbr_frames)>5
    order=[];
    for xnd=1:length(nbr_frames)
        if nbr_frames(xnd)==20000
            if xnd==length(nbr_frames)
                order(xnd)=3;
            else
                order(xnd)=1;
            end
        else
            order(xnd)=2;
        end
    end
else
    order=[1 2 1 2 3];
end
a=1:length(order);
order(max(a(order==2))+1:end)=3;
nbr_exp=length(order);
nbr_bas=sum(order==3);
bas_exp=[];
for lnd=1:length(nbr_bas)
    bas_exp(lnd)=shutterOpen((nbr_exp*2)-((lnd*2)-1));
end
bas_exp=min(bas_exp);


[baseline]=baseline_f(act,cur_frame_times,aux_data,velM_ind,nbr_frames,bas_exp);
% baseline=median(act);
raw_act=act/baseline;
% raw_act=raw_act-1;
% thresh=det_thresh_for_peak_resp(raw_act);
% raw_act(raw_act<thresh)=0;
ROI_act=map_ROI_act_to_aux_data(raw_act,cur_frame_times,length(aux_data));

results=struct;
results.snips={};
results.stim={};
results.runM={};
results.velM={};
results.runP={};
cnt=1;
cnt2=1;
for tnd=1:length(order)
    counter=(tnd*2)-1;
    if order(tnd)==1
%         display(['feedback ' num2str(cnt) ' *************************************'])
%         [fb_snips,fb_stim,fb_runM,fb_velM]=fb_calc(stim_id,velM_ind,velM_raw,ROI_act,ids,1500,500,shutterOpen,counter);
        [fb_snips,fb_runM,fb_stim,fb_velM,~]=fb_calc(stim_id,velM_ind,ids,15,5,shutterOpen,counter,ROI_act,velM_raw);
        results.snips{tnd}=fb_snips;
        results.stim{tnd}=fb_stim;
        results.runM{tnd}=fb_runM;
        results.velM{tnd}=fb_velM;
        cnt=cnt+1;
    elseif order(tnd)==2
%         display(['playback ' num2str(cnt2) ' *************************************'])
%         [pb_snips,pb_stim,pb_runM,pb_runP,pb_velM]=pb_calc(stim_id,velM_ind,velM_raw,velP_ind,ROI_act,ids,1500,1000,shutterOpen,counter);
        [pb_snips,pb_runM,pb_stim,pb_runP,pb_velM,~]=pb_calc(stim_id,velM_ind,velP_ind,ids,15,10,shutterOpen,counter,ROI_act,velM_raw);
        results.snips{tnd}=pb_snips;
        results.stim{tnd}=pb_stim;
        results.runM{tnd}=pb_runM;
        results.velM{tnd}=pb_velM;
        results.runP{tnd}=pb_runP;
        cnt2=cnt2+1;
    end
end

snips=results.snips;

curYmax=0;
for ynd=1:length(results.snips)
    snipy=[];
    for pnd=1:length(ids);
        if isempty(results.snips{ynd}{pnd})
            snipy(pnd)=0;
        else
            snipy(pnd)=max(max(results.snips{ynd}{pnd}));
        end
    end
    curYmax=max([curYmax snipy]);
end
curYmax=max([curYmax 2]);

x_val=[1:l_win+r_win+1];


if draw_raw==1
    sequ=[1 2 3 8 9 10 4 5 6 11 12 13 7];
    for ind=1:length(results.snips)
        if order(ind)==1
            color=[1 0 0];
        elseif order(ind)==2
            color=[0 0 0];
        end
        figure;
        set(gcf,'position',fig_pos);
        for knd=1:length(sequ)
            subplot(2,7,sequ(knd))
            if knd==1
                if order(ind)==1
                    title(['feedback'])
                elseif order(ind)==2
                    title(['playback'])
                end
            end
            rectangle('position',[15 0.5 10 3],'facecolor',[0.95 0.95 0.95],'linestyle','none')
            if ~isempty(results.snips{ind}{knd})
                hold on
                plot(results.snips{ind}{knd},'color',color)
            end
            if ~isempty(results.stim{ind}{knd})
                hold on
                plot(results.stim{ind}{knd}/6,'color',[0 0 1])
            end
            if ~isempty(results.runM{ind}{knd})
                hold on
                plot(results.runM{ind}{knd}/4,'color',[1 0 0])
            end
            if order(ind)==2
                if ~isempty(results.runP{ind}{knd})
                    hold on
                    plot((results.runP{ind}{knd}/4)+0.6,'color',[0 1 0])
                end
            end
            drawnow
            axis tight
            %             axis off
            %             box off
            ylim([-1 curYmax])
            xlim([0 max(x_val)])
        end
    end
end
if draw_vel==1
    max_size=0;
    for fnd=1:length(results.snips)
        for vnd=1:size(results.snips{fnd},2)
            max_size=max([max_size size(results.snips{fnd}{vnd},2)]);
        end
    end
    
    for vnd=1:length(results.snips)
        figure;
        set(gcf,'position',fig_pos);
        for snd=1:length(ids)
            stop=0;
            tnd=snd;
            cnt=1;
            if ~isempty(results.snips{vnd}{snd})
                while stop==0
                    hold on
                    subplot(max_size,length(ids),tnd)
                    hold on
                    rectangle('position',[1500 0.5 1000 4],'facecolor',[0.95 0.95 0.95],'linestyle','none')
                    hold on
                    plot(results.velM{vnd}{snd}(:,cnt)*5,'color',[0 0 0])
                    hold on
                    plot(results.snips{vnd}{snd}(:,cnt),'color',[1 0 0])
                    if cnt==size(results.snips{vnd}{snd},2)
                        stop=1;
                    else
                        cnt=cnt+1;
                        tnd=tnd+length(ids);
                    end
                    axis tight
                    ylim([-1 curYmax])
                    xlim([-0.5 max(x_val)])
                    axis off
                end
            end
        end
    end
end

fb_snips_av={};
pb_snips_av={};;
for cnd=1:length(ids)
    fb_av=[];
    pb_av=[];
    cnt=0;
    cnt2=0;
    for mnd=1:length(results.snips)
        if order(mnd)==1
            if ~isempty(results.snips{mnd}{cnd})
                fb_av(:,cnt+1:cnt+size(results.snips{mnd}{cnd},2))=results.snips{mnd}{cnd};
                cnt=cnt+size(results.snips{mnd}{cnd},2);
            end
        elseif order(mnd)==2
            if ~isempty(results.snips{mnd}{cnd})
                pb_av(:,cnt2+1:cnt2+size(results.snips{mnd}{cnd},2))=results.snips{mnd}{cnd};
                cnt2=cnt2+size(results.snips{mnd}{cnd},2);
            end
        end
    end
    fb_snips_out{cnd}=fb_av;
    pb_snips_out{cnd}=pb_av;
    if ~isempty(fb_av)
        if size(fb_av,2)>1
            fb_snips_av{cnd}=mean(fb_av');
        else
            fb_snips_av{cnd}=fb_av';
        end
    else
        fb_snips_av{cnd}=[];
    end
    if ~isempty(pb_av)
        if size(pb_av,2)>1
            pb_snips_av{cnd}=mean(pb_av');
        else
            pb_snips_av{cnd}=pb_av';
        end
    else
        pb_snips_av{cnd}=[];
    end
end

if draw_last==1
    sequ=[1 2 3 8 9 10 4 5 6 11 12 13 7];
    figure;%('color',[1 1 1]);
    set(gcf,'position',fig_pos);
    for ind=1:length(ids)
        subplot(2,7,sequ(ind))
        if ind==1
            if nargin>7
                title(['cell ' num2str(curZ(1,1)) ' z: ' num2str(curZ(1,2))])
            else
                title(['cell ' num2str(curZ(1,1))])
            end
        end
        rectangle('position',[1500 0 1000 4],'facecolor',[0.95 0.95 0.95],'linestyle','none')
        if ~isempty(fb_snips_av{ind})
            hold on
            plot(fb_snips_av{ind},'color',[1 0 0],'LineWidth',2)
        end
        if ~isempty(pb_snips_av{ind})
            hold on
            plot(pb_snips_av{ind},'color',[0 0 0],'LineWidth',2)
        end
        axis tight
        axis off
        box off
        ylim([0 curYmax])
        xlim([0 r_win+l_win+1])
        drawnow
    end
end